package com.example.mygrade.webapi;

import com.example.mygrade.core.PageUtils;
import com.example.mygrade.core.RUtil;
import com.example.mygrade.domain.ClassInfo;
import com.example.mygrade.domain.Dept;
import com.example.mygrade.domain.Student;
import com.example.mygrade.service.ClassInfoService;
import com.example.mygrade.service.DeptService;
import com.example.mygrade.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/classinfo")
public class ClassInfoController {
    @Autowired
    private ClassInfoService classInfoService;

    /**
     * 读取得url:"/webapi/teacher/list"
     */
    @GetMapping("/list")
    public List<ClassInfo> getAll(){
        List<ClassInfo> classInfos = classInfoService.findAll();
        return classInfos;
    }

    /**
     * 分页读取全部内容:"/webapi/teacher/getbypage"
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(@RequestParam(value = "page",defaultValue="0") Integer page,
                       @RequestParam(value = "size",defaultValue = "10") Integer size,
                       @RequestParam(value = "passage",defaultValue = "") String passage,
                       @RequestParam(value = "chooseValue",defaultValue = "") String chooseQuery,
                       @RequestParam(value = "sort",defaultValue = "id") String sortPassage,
                       @RequestParam(value = "direct",defaultValue = "ASC") String direct
    ){
        Page<ClassInfo> classInfoPage;
        Sort sort;
        if (direct.equals("asc")){
            sort = Sort.by(Sort.Direction.ASC,sortPassage);
        }else{
            sort = Sort.by(Sort.Direction.DESC,sortPassage);
        }
        if(ObjectUtils.isEmpty(passage)){
        Pageable pageable = PageRequest.of(page,size, sort);
        classInfoPage = classInfoService.findAll(pageable);
        }else {
            switch (chooseQuery) {
                case "1": {
                    ClassInfo classInfo = new ClassInfo();
                    classInfo.setClassid(Integer.parseInt(passage));
                    Pageable pageable = PageRequest.of(page, size, sort);
                    ExampleMatcher matcher = ExampleMatcher.matching()
                            .withMatcher("classid", ExampleMatcher.GenericPropertyMatchers.contains());
                    Example<ClassInfo> example = Example.of(classInfo, matcher);
                    classInfoPage = classInfoService.findAll(example, pageable);
                    break;
                }
                case "2": {
                    ClassInfo classInfo = new ClassInfo();
                    classInfo.setGradeid(Integer.parseInt(passage));
                    Pageable pageable = PageRequest.of(page, size, sort);
                    ExampleMatcher matcher = ExampleMatcher.matching()
                            .withMatcher("gradeid", ExampleMatcher.GenericPropertyMatchers.contains());
                    Example<ClassInfo> example = Example.of(classInfo, matcher);
                    classInfoPage = classInfoService.findAll(example, pageable);
                    break;
                }
                default: {
                    ClassInfo classInfo = new ClassInfo();
                    classInfo.setMid(passage);
                    Pageable pageable = PageRequest.of(page, size, sort);
                    ExampleMatcher matcher = ExampleMatcher.matching()
                            .withMatcher("mid", ExampleMatcher.GenericPropertyMatchers.contains());
                    Example<ClassInfo> example = Example.of(classInfo, matcher);
                    classInfoPage = classInfoService.findAll(example, pageable);
                    break;
                }
            }

        }


        PageUtils pageUtils = new PageUtils(classInfoPage.getContent(),classInfoPage.getTotalElements());
        return RUtil.success(pageUtils,"获取成功");
    }



    /**
     * 修改之前获取学生信息，读取的url:/webapi/teacher/get
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        ClassInfo classInfo = classInfoService.findById(id);
        return RUtil.success(classInfo,"获取成功");
    }

    /**
     * 新增学生的方法/webapi/teacher/insert
     * @param classInfo
     * @return
     */
    @PostMapping("/insert")
    public R insert(ClassInfo classInfo){
        ClassInfo classInfo1 = classInfoService.insert(classInfo);
        return RUtil.success(classInfo1,"插入成功");
    }

    /**
     * 更新学生的方法/webapi/teacher/update
     * @param classInfo
     * @return
     */
    @PutMapping("/update")
    public R update(ClassInfo classInfo){
        ClassInfo classInfo1 = classInfoService.update(classInfo);
        return RUtil.success(classInfo1,"更新成功");
    }

    /**
     * 删除学生的方法/webapi/teacher/delete
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        classInfoService.delete(id);
        return RUtil.success(null,"删除成功");
    }

    /**
     * 批量删除学生
     * @param arr
     */
    @DeleteMapping("/batchdelete/{arr}")
    public R delete(@PathVariable Integer[] arr){

        classInfoService.delete(arr);
        return RUtil.success(null,"批量删除成功");
    }

    /**
     * 批量导入课程
     * @param file
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/importClassinfo")
    @ResponseBody
    public R importClassinfo(@RequestParam MultipartFile[] file, HttpSession session) throws Exception{
        int result = 0;
        try {
            result = classInfoService.insertClassinfo(file[0]);
        }catch (Exception e){
            e.printStackTrace();
        }

        if (result > 0){
            return RUtil.success(result,"导入成功");
//            return "{\"result\":\"excel文件数据导入成功！\"}";
        }else{
//            return "{\"result\":\"excel文件数据导入失败！\"}";
            return RUtil.error(400,"导入失败");
        }
    }
}
